#ifndef INITDB_H
#define INITDB_H

#include <QtSql>

/*
 *
 * firstname varchar(40),
  lastname varchar(60),
  chnname nvarchar(48),
  birthday char(128),
  birthplace char(128),
  nationality char(128),
  occupation char(128),
  identificationtype char(128),
  identification char(128),
  identificationday char(128),
  entrycause char(128)
*/
void addApplyPersion(QSqlQuery &q,
                     const QString &firstname,
                     const QString &lastname,
                     const QString &chnname,
                     const QString &birthday,
                     const QString &birthplace,
                     const QString &nationality,
                     const QString &occupation,
                     const QString &identificationtype,
                     const QString &identification,
                     const QString &identificationday,
                     const QString &entrycause)
{
    q.addBindValue(firstname);
    q.addBindValue(lastname);
    q.addBindValue(chnname);
    q.addBindValue(birthday);
    q.addBindValue(birthplace);
    q.addBindValue(nationality);

    q.addBindValue(occupation);
    q.addBindValue(identificationtype);
    q.addBindValue(identification);
    q.addBindValue(identificationday);
    q.addBindValue(entrycause);

    q.exec();
}

QSqlError initOtherDb()
{
    QSqlDatabase otherdb = QSqlDatabase::addDatabase("QSQLITE");
    otherdb.setDatabaseName(":/file/student.db");

    if (!otherdb.open())
        return otherdb.lastError();

    QStringList otherTables = otherdb.tables();
    if (otherTables.contains("Burn", Qt::CaseInsensitive))
        return QSqlError();

    return QSqlError();
}

QSqlError initDb()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","bbb");
    db.setDatabaseName(":memory:");

    if (!db.open())
        return db.lastError();

    QStringList tables = db.tables();
    if (tables.contains("ApplyForm", Qt::CaseInsensitive))
        return QSqlError();

    QSqlQuery query;
    //29个字段
    QString sqlStr = QLatin1String("CREATE TABLE ApplyForm (\
                                   id integer PRIMARY KEY AUTOINCREMENT NOT NULL,\
                                   groupnum varchar(11),\
                                   docType varchar(2),\
                                   num varchar(2),\
                                   \
                                   firstname varchar(40),\
                                   lastname varchar(60),\
                                   \
                                   chtname nvarchar(48),\
                                   sexy varchar(2),\
                                   birthday char(128),\
                                   \
                                   burn char(128),\
                                   nationality char(128),\
                                   \
                                   certtype char(128),\
                                   cert char(128),\
                                   certdate char(128),\
                                   \
                                   certtime char(128),\
                                   company char(128),\
                                   \
                                   career char(128),\
                                   destination char(128),\
                                   totalnum char(128),\
                                   \
                                   signtype char(128),\
                                   signtime char(128),\
                                   signdate char(128),\
                                   \
                                   reason char(128),\
                                   carrysign char(128),\
                                   category char(128),\
                                   \
                                   stayday char(128),\
                                   staydate char(128),\
                                   \
                                   import char(128),\
                                   certaddress char(128),\
                                   groupnum1 char(128),\
                                   \
                                   agentnum char(128),\
                                   agent char(128),\
                                   \
                                   invitenum char(128),\
                                   invite char(128),\
                                   \
                                   receiver char(128),\
                                   receivercontact char(128),\
                                   \
                                   photo char(128)\
                                   );");

    if (!query.exec(sqlStr)){
        return query.lastError();
    }
    return QSqlError();
}


#endif
